 var appPath = getAppPath();

$(function(){
	// 检索日期范围
//	$("#queryDateStart").datebox({
//		formatter:dtFmt,
//		parser:dtParser
//	});
//	$("#queryDateEnd").datebox({
//		formatter:dtFmt,
//		parser:dtParser
//	});
	getAllUserDocCnt();
	initJspGrid();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);//格式化mm-dd
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

// 列表初始化
function initJspGrid(){
	$('#infoGrid').datagrid({
	    url:appPath + "/itbStats/getNewUserDocInfoList.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[	     
	        {field:'month',title:'月份',width:60,align:'left',sortable:false,
	        	  formatter:function(value, rowData, rowIndex){ 
	        		  if(value){
	        			  return value.substr(0,4)+'年'+value.substr(4,value.length)+'月';
	        		  }
	        		  return 'N/A';
	        	  }
	        },
	        {field:'week',title:'周次',width:60,align:'left',sortable:false,
	         	  formatter:function(value, rowData, rowIndex){
	        		  if(value && rowData.grpBy!='month'){
	        			  return value.substr(0,4)+'年第'+value.substr(4,value.length)+'周';
	        		  }
	        		  return 'N/A';
	        	  }
	        },
//	        {field:'day',title:'日期',width:60,align:'left',sortable:false,
//	         	  formatter:function(value, rowData, rowIndex){ 
//	        		  return 'N/A'
//	        	  }
//	        },	      
//	        {field:'ppt_cnt',title:'PPT文档数',width:60,align:'left',sortable:false,
//		  	   	  formatter:function(value, rowData, rowIndex){ 
//		  	   		return '<span style="color:blue">'+Number(value||'0')+'</span>';
//			  	  }
//	        },	      
	        {field:'ppt_pg_cnt',title:'PPT页数',width:60,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 
		  	   		return '<span style="color:blue">'+Number(value||'0')+'</span>';
			  	  }
	        },
//	        {field:'tg_cnt',title:'TG文档数',width:60,align:'left',sortable:false,
//		  	   	  formatter:function(value, rowData, rowIndex){ 	
//		  	   		return '<span style="color:blue">'+Number(value||'0')+'</span>';
//			  	  }
//	        },
	        {field:'tg_pg_cnt',title:'TG页数',width:60,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 	
		  	   		return '<span style="color:blue">'+Number(value||'0')+'</span>';
			  	  }
	        },
	        {field:'pg_cnt',title:'文档页总数',width:60,align:'left',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){
		  	   		var num = Number(rowData.ppt_pg_cnt||'0')+Number(rowData.tg_pg_cnt||'0');
		  	   		return '<span style="color:red">'+num+'</span>';
			  	  }
	        }
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	});
}

function getAllUserDocCnt() {
	var url = appPath+"/itbStats/getAllUserDocCnt.action";
	var sendData = {
	}
	$.post(url,sendData,function(ret){
		if(ret){
			//$("#pptDocTolCnt").html(ret.ppt_cnt);
			$("#pptPgTolCnt").html(ret.ppt_pg_cnt);
			//$("#tgDocTolCnt").html(ret.tg_cnt);
			$("#tgPgTolCnt").html(ret.tg_pg_cnt);
			$("#pgTolCnt").html(Number(ret.ppt_pg_cnt||'0')+Number(ret.tg_pg_cnt||'0'));
		}else{
			$.messager.alert('提示信息','用户文档总数获取失败！','info');
			//$("#pptDocTolCnt").html('');
			$("#pptPgTolCnt").html('');
			//$("#tgDocTolCnt").html('');
			$("#tgPgTolCnt").html('');
			$("#pgTolCnt").html('');
		}
	});
}

function getQueryParam(){
	var queryObject = {};
//	var findDtS = $("#queryDateStart").datebox('getValue');
//	var findDtE = $("#queryDateEnd").datebox('getValue');
	var grpBy = $("#queryGrpBy").combobox('getValue');
	
//	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS +' 00:00:00 ';
//	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	if(grpBy && grpBy.length>0) queryObject.grpBy = grpBy;
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	getAllUserDocCnt();
	$("#infoGrid").datagrid('unselectAll');
	$("#infoGrid").datagrid('load', getQueryParam());
}


//获得某周的开始日期　　 
function getWeekStartDate(paraYear,paraMonth,paraDay,paraDayOfWeek){
　 var weekStartDate = new Date(paraYear, paraMonth, paraDay + 1 - paraDayOfWeek);
　 return formatDate(weekStartDate);
}

//获得某周的结束日期　　 
function getWeekEndDate(paraYear,paraMonth,paraDay,paraDayOfWeek){
　 var weekEndDate = new Date(paraYear, paraMonth, paraDay + (7 - paraDayOfWeek));
　 return formatDate(weekEndDate);
}
//获得某月的开始日期　　 
function getMonthStartDate(paraYear,paraMonth){
　 var monthStartDate = new Date(paraYear, paraMonth, 1);　　 
　 return formatDate(monthStartDate);
}
//获得某月的结束日期　　 
function getMonthEndDate(paraYear,paraMonth){
　 var monthEndDate = new Date(paraYear,paraMonth, getMonthDays(paraYear,paraMonth));
　 return formatDate(monthEndDate);
}

//格式化
function formatDate(date) {　　　 
　 var myyear = date.getFullYear();
　 var mymonth = date.getMonth()+1;
　 var myweekday = date.getDate();

　 if(mymonth < 10){　　 
　　　 mymonth = "0" + mymonth;　　 
　 }
　 if(myweekday < 10){　　 
　　　 myweekday = "0" + myweekday;　　 
　 }
　 return (myyear+"-"+mymonth + "-" + myweekday);
}　

//获得某月的天数
function getMonthDays(paraYear,paraMonth){
　 var monthStartDate = new Date(paraYear, paraMonth, 1);
　 var monthEndDate = new Date(paraYear, paraMonth + 1, 1);
　 var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);　　　 
　 return days;　　　 
}　



